package cn.clq.org.service.impl;

import cn.clq.basic.query.BaseQuery;
import cn.clq.basic.service.impl.BaseServiceImpl;
import cn.clq.basic.util.PageList;
import cn.clq.org.domain.Department;
import cn.clq.org.domain.Employee;
import cn.clq.org.mapper.DepartmentMapper;
import cn.clq.org.query.DepartmentQuery;
import cn.clq.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author clqlongbao
 * @version 1.0
 * @description: 部门CRUD接口的实现类
 * @date 2022/6/1 13:14
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department>  implements IDepartmentService {
    //基础的CRUD实现通过继承BaseServiceImpl得到

    //特殊的业务实现需求自己写
    @Autowired
    private DepartmentMapper departmentMapper;
    /**
     * @description:新增
     * @param: department
     * @return: void
     * @author clqlongbao
     * @date: 2022/6/6 18:29
     */
    @Override
    @Transactional
    public void save(Department department) {//返回自增ID
        departmentMapper.save(department);
        this.update(department);
    }
    /**
     * @description:修改
     * @param: department
     * @return: void
     * @author clqlongbao
     * @date: 2022/6/6 18:29
     */
    @Override
    @Transactional
    public void update(Department department) {
        //在数据库保存dirpath
        String dirPath = "";
        if(department.getParent() == null){
            //1.新增的是顶级部门  /自己id
            dirPath = "/"+department.getId();
        }else{
            //2.新增的是非顶级部门    找上级部门的dirpath + /自己id
            Department parent = departmentMapper.loadById(department.getParent().getId());
            dirPath = parent.getDirPath() + "/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.updateById(department);
    }

    /**
     * @description:查找部门的无限级树
     * @param:
     * @return: java.util.List<cn.clq.org.domain.Department>
     * @author clqlongbao
     * @date: 2022/6/7 16:10
     */
    @Override
    public List<Department> deptTree() {
        //1.先查询所有数据
        List<Department> departments = departmentMapper.loadAll();
        //为了后面方便找自己的父亲，这里将list转换为map，后面利用k值来找父亲，
        //将部门的id作为k，将当前行的这个部门对象作为v
        Map<Long, Department> alltDepts = departments.stream().collect(Collectors.toMap(Department::getId, department -> department));
        //2.找到所有第一层级的部门
        List<Department> firstDepts = new ArrayList<>();
        for (Department tem : departments) {
            if(tem.getParent_id() == null){
                firstDepts.add(tem);
            }else{
                //3.如果不是第一层级，就找自己的父亲
                Department parent = alltDepts.get(tem.getParent_id());
                if(parent != null){
                   parent.getChildren().add(tem);
                }
            }
        }
        return firstDepts;
    }
}
